package ae.generator;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 实体类生成
 * @author: zxm
 * @date: 2025/9/4
 */
public class EntityGenerator {


    public String createEntityContent(String tableName,String schemaPattern,String className){
        List<ColumnInfo> tableColumns = getTableColumns(tableName,schemaPattern);
        return generateEntityContent(className, tableColumns);
    }


    /**
     * 生成实体类内容
     */
    private String generateEntityContent(String className, List<ColumnInfo> columns) {
        StringBuilder sb = new StringBuilder();
        // 字段定义
        for (ColumnInfo column : columns) {
            // 字段注释
            if (column.getRemarks() != null && !column.getRemarks().isEmpty()) {
                sb.append("    /**\n");
                sb.append("     * ").append(column.getRemarks()).append("\n");
                sb.append("     */\n");
            }

            // 字段定义
            sb.append("    private ").append(column.getJavaType()).append(" ").append(column.getJavaFieldName()).append(";\n\n");
        }

        // 无参构造函数
        sb.append("    /**\n");
        sb.append("     * 无参构造函数\n");
        sb.append("     */\n");
        sb.append("    public ").append(className).append("() {\n");
        sb.append("    }\n\n");

        // Getter和Setter方法
        for (ColumnInfo column : columns) {
            // Getter方法
//            sb.append("    /**\n");
//            sb.append("     * 获取").append(column.getRemarks() != null ? column.getRemarks() : column.getJavaFieldName()).append("\n");
//            sb.append("     * @return ").append(column.getJavaFieldName()).append("\n");
//            sb.append("     */\n");
            sb.append("    public ").append(column.getJavaType()).append(" get").append(
                    GenerateUtil.firstLetterToUpper(column.getJavaFieldName())).append("() {\n");
            sb.append("        return this.").append(column.getJavaFieldName()).append(";\n");
            sb.append("    }\n\n");
            // Setter方法
//            sb.append("    /**\n");
//            sb.append("     * 设置").append(column.getRemarks() != null ? column.getRemarks() : column.getJavaFieldName()).append("\n");
//            sb.append("     * @param ").append(column.getJavaFieldName()).append(" ").append(column.getRemarks() != null ? column.getRemarks() : "").append("\n");
//            sb.append("     */\n");
            sb.append("    public void set").append(
                    GenerateUtil.firstLetterToUpper(column.getJavaFieldName())).append("(").append(column.getJavaType()).append(" ").append(
                    column.getJavaFieldName()).append(") {\n");
            sb.append("        this.").append(column.getJavaFieldName()).append(" = ").append(column.getJavaFieldName()).append(";\n");
            sb.append("    }\n\n");
        }

        return sb.toString();
    }


    /**
     * 生成字段信息
     */
    private List<ColumnInfo> getTableColumns(String tableName,String schemaPattern){
        try {
            List<ColumnInfo> columns = new ArrayList<>();
            OracleConnection oracleConnection = new OracleConnection();
            DatabaseMetaData metaData = oracleConnection.getDatabaseMetaData("jdbc.properties");
            ResultSet rs = metaData.getColumns(null, schemaPattern, tableName.toUpperCase(), null);
            while (rs.next()) {
                var column = new ColumnInfo();
                // 列名
                column.setColumnName(rs.getString("COLUMN_NAME"));
                // 数据类型
                column.setDataType(rs.getInt("DATA_TYPE"));
                // 类型名称
                column.setTypeName(rs.getString("TYPE_NAME"));
                // 注释
                column.setRemarks(rs.getString("REMARKS"));
                // 是否可为空
                column.setNullable(rs.getBoolean("NULLABLE"));
                // 转换为Java类型
                column.setJavaType(GenerateUtil.getJavaType(column.getDataType()));
                // 转换为Java属性名（下划线转驼峰）
                column.setJavaFieldName(GenerateUtil.convertToCamelCase(column.getColumnName()));
                columns.add(column);
            }
            return columns;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
